package com.base.api;

import com.base.api.exception.MessageRunTimeException;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 建造者模式
 */
public class MessageBuilder {
    private String messageId;
    private  String topic;
    private String routingKey="";
    private Map<String,Object> attribute=new HashMap<String,Object>();
    private int delayMills;
    private String messageType=MessageType.CONFRIM;

    /**
     * 构造函数私有化
     */
    private MessageBuilder(){

    }


    public static MessageBuilder create(){
        return new MessageBuilder();
    }

    public MessageBuilder withMessageId(String messageId){
        this.messageId=messageId;
        return this;
    }

    public MessageBuilder withTopic(String topic){
        this.topic=topic;
        return this;
    }

    public MessageBuilder withRoutingKey(String routingKey){
        this.routingKey=routingKey;
        return this;
    }


    public MessageBuilder withAttribute(String key,Object value){
        this.attribute.put(key,value);
        return this;
    }

    public MessageBuilder withAttributes(Map<String,Object> attribute){
        this.attribute=attribute;
        return this;
    }


    public MessageBuilder withDelayMills(int delayMills){
        this.delayMills=delayMills;
        return this;
    }

    public MessageBuilder withMessageType(String messageType){
        this.messageType=messageType;
        return this;
    }


    /**
     * 本身对象
     * @return
     */
    public Message build(){
        //1、 check messageId
        if(messageId==null){
            messageId= UUID.randomUUID().toString();
        }
        //2、 topic is null
        if(topic==null){
           throw new MessageRunTimeException("topic is null");
        }
        Message message=new Message(messageId,topic,routingKey,attribute,delayMills,messageType);
        return message;
    }



}
